package com.REPDPMSystem.Controller;

import com.REPDPMSystem.Service.FileService;
import com.REPDPMSystem.Utils.R;
import com.REPDPMSystem.Entity.MyFile;
import com.alibaba.fastjson2.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;


@RestController
@CrossOrigin
public class FileController {
    @Autowired
    FileService fileService;


    @GetMapping("/GET/FileInfoByName")
    @ResponseBody
    public R getFileInfoByName(@RequestParam(value = "name")String name,@RequestParam(value = "folder_id")String folder_id){
        return R.ok().data("FileInfoList",new JSONArray(fileService.getFileInfoByName(name,folder_id)));
    }
    //需要获取文件类型
    //OK
    @PostMapping("/POST/fileUpload")
    @ResponseBody
    public R fileUpload(@RequestParam(value = "file_name")String file_name,
                        @RequestParam(value = "folder_id")String folder_id,
                        @RequestParam(value = "staff_id")String staff_id,
                        @RequestParam(value = "multipartFile")MultipartFile multipartFile) throws Exception {
        MyFile myFile = new MyFile();
        myFile.setFile_name(file_name);
        myFile.setFolder_id(folder_id);
        myFile.setStaff_id(staff_id);
        myFile.setDate(System.currentTimeMillis());
        return fileService.upLoad(myFile,multipartFile)? R.ok():R.error().message("上传文件失败！");
    }
//TODO:前端新打开一个页面
    //TODO:前端获取错误

    @GetMapping ("/GET/fileDownLoad")
    @ResponseBody
    public void fileDownLoad(HttpServletResponse response,  @RequestParam String filePath) {
        System.out.println("123");
        fileService.downLoad(response, filePath);
        return;
    }
    @GetMapping("/GET/FileInfoByFolder")
    @ResponseBody
    public R getFileInfoByFolder(@RequestParam(value = "folder_id") String folder_id){
        return R.ok().data("FileInfoList",new JSONArray(fileService.getFileByFolderId(folder_id)));
    }
    @PostMapping("/POST/deleteFileByFileId")
    @ResponseBody
    public R deleteFileByFileId(@RequestParam(value = "file_id") String file_id){
        return fileService.deleteFileByFileId(file_id) ? R.ok() : R.error().message("删除失败！");
    }


}
